package com.it2297.view;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.it2297.controller.NeedyController;
import com.it2297.controller.WorkshopController;
import com.it2297.model.Trainer;

import java.awt.Font;
import java.awt.ScrollPane;
import java.awt.Label;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JSpinner;
import javax.swing.JComboBox;

public class WorkshopSearchView extends JPanel {
private final WorkshopController search=new WorkshopController(this);
	
	
	
	private final JLabel lblNewLabel = new JLabel("Workshop Search");
	private final JScrollPane scrollPane_1_1 = new JScrollPane();
	private final JPanel panel = new JPanel();
	private final JButton btnSearch = new JButton("Search");
	private final JTextField workshopidtxt;
	private final JTextField locationtxt;
	private final  JTextField titletxt;
	private final JSpinner capacity=new JSpinner();
	private final  JSpinner slot=new JSpinner();
	private final  JComboBox trainerBox=new JComboBox();
	private final JTextField dstxt;
	private final JTextField detxt;
	private final JTextField descriptionarea;
	private final JLabel lblWorkshopId = new JLabel("Workshop Id:");
	private final JLabel lblDateStart = new JLabel("Date Start:");
	private final JLabel lblDateEnd = new JLabel("Date End:");
	private final JLabel lblDescription = new JLabel("Description:");
	private final JLabel lblLocation = new JLabel("Location:");
	private final JLabel lblCapacity = new JLabel("Capacity:");
	private final JLabel lblAvailableSlots = new JLabel("Available Slots:");
	private final JLabel lblTrainerNric = new JLabel("Trainer NRIC");
	
	/**
	 * Create the panel.
	 */
	public WorkshopSearchView() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(20, 44, 825, 519);
		add(panel);
		panel.setLayout(null);
		
		Label label = new Label("Please Search By Title");
		label.setFont(new Font("Calibri", Font.ITALIC, 16));
		label.setBounds(10, 10, 184, 22);
		panel.add(label);
		
		Label workshoplbl = new Label("Workshop Title:");
		workshoplbl.setFont(new Font("Arial", Font.ITALIC, 14));
		workshoplbl.setBounds(10, 57, 125, 22);
		panel.add(workshoplbl);
		
		titletxt = new JTextField();
		titletxt.setBounds(179, 59, 383, 20);
		panel.add(titletxt);
		titletxt.setColumns(10);
		
		
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(titletxt.getText().isEmpty()){
					JOptionPane.showMessageDialog(null,"Please Enter Workshop Title");  
					
				}
				else{
					
					String title=titletxt.getText();
					
					lblWorkshopId.setVisible(true);
					lblDateStart.setVisible(true);
					lblDateEnd.setVisible(true);
					lblDescription.setVisible(true);
					lblLocation.setVisible(true);
					lblCapacity.setVisible(true);
					lblAvailableSlots.setVisible(true);
					lblTrainerNric.setVisible(true);
					workshopidtxt.setVisible(true);
					dstxt.setVisible(true);
					detxt.setVisible(true);
					descriptionarea.setVisible(true);
					locationtxt.setVisible(true);
					capacity.setVisible(true);
					slot.setVisible(true);
					trainerBox.setVisible(true);
					
					
					search.getSearch(title);
				}
					
			}
		});
		btnSearch.setBounds(77, 485, 89, 23);
		panel.add(btnSearch);
		
		
		lblWorkshopId.setBounds(28, 109, 107, 14);
		lblWorkshopId.setVisible(false);
		panel.add(lblWorkshopId);
		
		
		lblDateStart.setBounds(28, 155, 89, 14);
		lblDateStart.setVisible(false);
		panel.add(lblDateStart);
		
		
		lblDateEnd.setBounds(28, 206, 107, 14);
		lblDateEnd.setVisible(false);
		panel.add(lblDateEnd);
		
		
		lblDescription.setBounds(28, 252, 107, 14);
		lblDescription.setVisible(false);
		panel.add(lblDescription);
		
		
		lblLocation.setBounds(28, 316, 89, 14);
		lblLocation.setVisible(false);
		panel.add(lblLocation);
		
		
		lblCapacity.setBounds(28, 357, 74, 14);
		lblCapacity.setVisible(false);
		panel.add(lblCapacity);
		
		
		lblAvailableSlots.setBounds(28, 399, 107, 14);
		lblAvailableSlots.setVisible(false);
		panel.add(lblAvailableSlots);
		
		
		lblTrainerNric.setBounds(28, 442, 107, 14);
		lblTrainerNric.setVisible(false);
		panel.add(lblTrainerNric);
		
		workshopidtxt = new JTextField();
		workshopidtxt.setBounds(179, 106, 383, 20);
		panel.add(workshopidtxt);
		workshopidtxt.setColumns(10);
		workshopidtxt.setVisible(false);
		
		dstxt = new JTextField();
		dstxt.setBounds(179, 152, 383, 20);
		panel.add(dstxt);
		dstxt.setColumns(10);
		dstxt.setVisible(false);
		
		detxt = new JTextField();
		detxt.setBounds(179, 203, 383, 20);
		panel.add(detxt);
		detxt.setColumns(10);
		detxt.setVisible(false);
		
		descriptionarea = new JTextField();
		descriptionarea.setBounds(179, 252, 383, 40);
		panel.add(descriptionarea);
		descriptionarea.setColumns(10);
		descriptionarea.setVisible(false);
		
		locationtxt = new JTextField();
		locationtxt.setBounds(179, 313, 383, 20);
		panel.add(locationtxt);
		locationtxt.setColumns(10);
		locationtxt.setVisible(false);
		
		
		capacity.setBounds(179, 354, 184, 20);
		capacity.setVisible(false);
		panel.add(capacity);
		
		
		slot.setBounds(179, 396, 184, 20);
		slot.setVisible(false);
		panel.add(slot);
		
		
		trainerBox.setBounds(179, 439, 184, 20);
		for(int i=0; i< Trainer.getMultiple().size();i++){
			trainerBox.addItem(Trainer.getMultiple().get(i).getNric());
			}
		trainerBox.setVisible(false);
		panel.add(trainerBox);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lblWorkshopId.setVisible(false);
				lblDateStart.setVisible(false);
				lblDateEnd.setVisible(false);
				lblDescription.setVisible(false);
				lblLocation.setVisible(false);
				lblCapacity.setVisible(false);
				lblAvailableSlots.setVisible(false);
				lblTrainerNric.setVisible(false);
				workshopidtxt.setVisible(false);
				dstxt.setVisible(false);
				detxt.setVisible(false);
				descriptionarea.setVisible(false);
				locationtxt.setVisible(false);
				capacity.setVisible(false);
				slot.setVisible(false);
				trainerBox.setVisible(false);
				titletxt.setText("");
			}
		});
		btnRefresh.setBounds(231, 485, 89, 23);
		panel.add(btnRefresh);
	
		
		JLabel lblNewLabel = new JLabel("Workshop Search");
		lblNewLabel.setFont(new Font("Tahoma", Font.ITALIC, 20));
		lblNewLabel.setBounds(10, 11, 221, 22);
		add(lblNewLabel);
		
	}
	public int getWorkshopId() {
		return Integer.parseInt(workshopidtxt.getText());
	}
	public void setWorkshopId(int workshopid){
		workshopidtxt.setText(Integer.toString(workshopid));
	}
	
	public String getTitle(){
		return titletxt.getText();
	}
	public void setTitle(String title){
		titletxt.setText(title);
	}
	public String getlocation() {
		return locationtxt.getText();
	}
	public void setlocation(String location){
		locationtxt.setText(location);
	}
	public int getCapacity(){
		return (int) capacity.getValue();
	}
	public void setCapacity(int totalcapacity){
		capacity.setValue(totalcapacity);
	}
	public int getAvailableSlot(){
		return (int) slot.getValue();
	}
	public void setAvailableSlot(int availableslot){
		slot.setValue(availableslot);
	}
	
	public String getDescription(){
		return descriptionarea.getText();
	}
	public void setDescription(String description){
		descriptionarea.setText(description);
	}
	public void setStartDate(Date dateStart){
		 if(!(dateStart == null)){
		 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		 String reportDate = df.format(dateStart);
		 dstxt.setText(reportDate);
		 }
	}
	
	
	public Date getStartDate(){
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
		Date date = null;
		try {
		  date=	(Date)formatter.parse(dstxt.getText());
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		System.out.println(date);
		 return date;
		
		
		
		
	}
		public Date getEndDate(){
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
			Date date=null;
			try {
				date = (Date)formatter.parse(detxt.getText());
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			System.out.println(date);
			 return date;
		
		}
		public void setEndDate(Date dateEnd){
			 if(!(dateEnd == null)){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			 String reportDate = df.format(dateEnd);
			 detxt.setText(reportDate);
			 }
		}
		public String getNRIC(){
			return (String) trainerBox.getSelectedItem();
		}
		public void setNRIC(String trainernric){
			trainerBox.setSelectedItem(trainernric);
		}
		
}
